package work.vcloud.baseinfo.controller;

import work.vcloud.frame.controller.BaseController;
import work.vcloud.frame.dao.OpenDao;
import work.vcloud.frame.vo.OutputVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONArray;
import java.util.*;

@RestController
@Scope("prototype")
public class RoleController extends BaseController {

	@Autowired
	OpenDao openDao;

	/**
	 * 修改查询应用信息
	 */
	@RequestMapping("api/role.insertUserRole")
	public OutputVo insertUserRole() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			JSONArray users = JSONArray.parseArray(map.get("users").toString());
			if(users.size() > 0) {
				for(int i = 0;i<users.size();i++) {
                    map.put("fk_user",users.get(i));
                    map.put("sqlMapId", "api_userrole.selectIfExit");
        			Integer result = (Integer) openDao.queryForObject(map);
        			if(result<=0) {
        				map.put("sqlMapId", "api_userrole.insert");
                        openDao.insert(map);
        			}
                }
			}
			
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	
	/**
	 * 绑定组织机构
	 */
	@RequestMapping("api/organization.insertUserOrganization")
	public OutputVo insertUserOrganization() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");
			JSONArray users = JSONArray.parseArray(map.get("users").toString());
			if(users.size() > 0) {
				for(int i = 0;i<users.size();i++) {
                    map.put("fk_user",users.get(i));
                    map.put("sqlMapId", "organization.insertUserOrganization");
                    openDao.insert(map);
                }
			}
			
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	
	
}
